package org.gruposp2p.dnie.model;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.PostLoad;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import static javax.persistence.EnumType.STRING;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.gruposp2p.dnie.model.xmladapter.DNIeUserAdapter;
import org.gruposp2p.dnie.model.xmladapter.DateAdapter;
import org.gruposp2p.dnie.utils.DateUtils;
import org.hibernate.search.annotations.DateBridge;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Resolution;

/**
 *
 * @author jj
 */
@Indexed
@XmlType
@XmlRootElement(name = "DnieDocumentToSign")
@XmlAccessorType(XmlAccessType.NONE)
@Entity
@Table(name = "DNIEDOCUMENTTOSIGN", catalog = "DNIE")
@NamedQueries({
    @NamedQuery(name = "DNIeDocumentToSign.findAll", query = "SELECT d FROM DNIeDocumentToSign d"),
    @NamedQuery(name = "DNIeDocumentToSign.findById", query = "SELECT d FROM DNIeDocumentToSign d WHERE d.id = :id"),
    @NamedQuery(name = "DNIeDocumentToSign.findByOptionSelectionType", query = "SELECT d FROM DNIeDocumentToSign d WHERE d.optionSelectionType = :optionSelectionType"),
    @NamedQuery(name = "DNIeDocumentToSign.findBySignatureDataType", query = "SELECT d FROM DNIeDocumentToSign d WHERE d.signatureDataType = :signatureDataType"),
    @NamedQuery(name = "DNIeDocumentToSign.findByTitle", query = "SELECT d FROM DNIeDocumentToSign d WHERE d.title = :title"),
    @NamedQuery(name = "DNIeDocumentToSign.findByUrl", query = "SELECT d FROM DNIeDocumentToSign d WHERE d.url = :url"),
    @NamedQuery(name = "DNIeDocumentToSign.findByDateEnd", query = "SELECT d FROM DNIeDocumentToSign d WHERE d.dateEnd = :dateEnd"),
    @NamedQuery(name = "DNIeDocumentToSign.findByDateBegin", query = "SELECT d FROM DNIeDocumentToSign d WHERE d.dateBegin = :dateBegin")})
public class DNIeDocumentToSign implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    @XmlElement
    @DocumentId
    private Integer id;
    @Basic(optional = false)
    @Lob
    @Column(name = "CONTENT", nullable = false)
    @XmlElement
    @Field
    private String content;
    @Basic(optional = false)
    @Column(name = "OPTIONSELECTIONTYPE", nullable = false, length = 255)
    @XmlElement
    @Enumerated(STRING)
    private DNIeOptionSelectionType optionSelectionType;
    @Basic(optional = false)
    @Column(name = "SIGNATUREDATATYPE", nullable = false, length = 255)
    @XmlElement
    @Enumerated(STRING)
    private DNIeDocumentType signatureDataType;
    @Basic(optional = false)
    @Column(name = "TITLE", nullable = false, length = 255)
    @XmlElement
    @Field
    private String title;
    @Basic(optional = false)
    @Column(name = "URL", nullable = true, length = 255)
    @XmlElement
    private String url;
    @Column(name = "DATECREATED")
    @Temporal(TemporalType.TIMESTAMP)
    @XmlElement
    @XmlJavaTypeAdapter(DateAdapter.class)
    @DateBridge( resolution = Resolution.DAY )
    private Date dateCreated;
    @Column(name = "DATEEND")
    @Temporal(TemporalType.TIMESTAMP)
    @XmlElement
    @XmlJavaTypeAdapter(DateAdapter.class)
    @DateBridge( resolution = Resolution.DAY )
    private Date dateEnd;
    @Basic(optional = false)
    @Column(name = "DATEBEGIN", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    @XmlElement
    @XmlJavaTypeAdapter(DateAdapter.class)
    @DateBridge( resolution = Resolution.DAY )
    private Date dateBegin;
    @XmlElementWrapper(name = "DNIeTags")
    @XmlElement(name = "DNIeTag", nillable=true)
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dnieDocumentToSignId")
    private Collection<DocumentTag> documentTagCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dnieDocumentToSignId")
    private Collection<DNIeSignedDocument> dnieSignedDocumentCollection;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "dnieDocumentToSignId")
    @XmlElementWrapper(name = "options")
    @XmlElement(name = "option", nillable=true)
    private Collection<DNIeDocumentToSignOption> dnieDocumentToSignOptionCollection;
    @JoinColumn(name = "DNIEUSER_ID", referencedColumnName = "ID", nullable = true)
    @ManyToOne(optional = false)
    @XmlElement
    @XmlJavaTypeAdapter(DNIeUserAdapter.class)
    private DNIeUser dnieUserId;
    @Basic(optional = false)
    @Column(name = "NUMBERSIGNATURES", nullable = false)
    @XmlElement
    private Integer numberSignatures = 0;
    @XmlElement
    private Boolean active = false;

    public DNIeDocumentToSign() {
    }

    public DNIeDocumentToSign(Integer id) {
        this.id = id;
    }

    public DNIeDocumentToSign(Integer id, String content,
            DNIeOptionSelectionType optionSelectionType, DNIeDocumentType signatureDataType,
            String title, String url, Date dateBegin) {
        this.id = id;
        this.content = content;
        this.optionSelectionType = optionSelectionType;
        this.signatureDataType = signatureDataType;
        this.title = title;
        this.url = url;
        this.dateBegin = dateBegin;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public DNIeOptionSelectionType getOptionSelectionType() {
        return optionSelectionType;
    }

    public void setOptionSelectionType(DNIeOptionSelectionType optionSelectionType) {
        this.optionSelectionType = optionSelectionType;
    }

    public DNIeDocumentType getSignatureDataType() {
        return signatureDataType;
    }

    public void setSignatureDataType(DNIeDocumentType signatureDataType) {
        this.signatureDataType = signatureDataType;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Date getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }

    public Date getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(Date dateEnd) {
        this.dateEnd = dateEnd;
    }

    public Date getDateBegin() {
        return dateBegin;
    }

    public void setDateBegin(Date dateBegin) {
        this.dateBegin = dateBegin;
    }

    public Collection<DocumentTag> getDocumentTagCollection() {
        return documentTagCollection;
    }

    public void setDocumentTagCollection(Collection<DocumentTag> documentTagCollection) {
        this.documentTagCollection = documentTagCollection;
    }

    public Collection<DNIeSignedDocument> getDnieSignedDocumentCollection() {
        return dnieSignedDocumentCollection;
    }

    public void setDnieSignedDocumentCollection(Collection<DNIeSignedDocument> dnieSignedDocumentCollection) {
        this.dnieSignedDocumentCollection = dnieSignedDocumentCollection;
    }

    public Collection<DNIeDocumentToSignOption> getDNIeDocumentToSignoptionCollection() {
        return dnieDocumentToSignOptionCollection;
    }

    public void setDNIeDocumentToSignoptionCollection(Collection<DNIeDocumentToSignOption> dnieDocumentToSignOptionCollection) {
        this.dnieDocumentToSignOptionCollection = dnieDocumentToSignOptionCollection;
    }

    public DNIeUser getDnieUserId() {
        return dnieUserId;
    }

    public void setDnieUserId(DNIeUser dnieUserId) {
        this.dnieUserId = dnieUserId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof DNIeDocumentToSign)) {
            return false;
        }
        DNIeDocumentToSign other = (DNIeDocumentToSign) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @PostLoad
    private void setActiveState() {
        Date todayDate = DateUtils.getTodayDate();
        if (todayDate.after(dateBegin) && todayDate.before(dateEnd)) active = true;
    }

    @Override
    public String toString() {
        return "dniedatabasemodel01.DNIeDocumentToSign[id=" + id + "]";
    }

}
